package com.dcbeida.activity.clerkmanager;


import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.dcbeida.R;
import com.dcbeida.activity.BaseActivity;
import com.dcbeida.adapter.AccountListAdapter;
import com.dcbeida.bean.Customer;
import com.dcbeida.fragment.BaseFragment;
import com.dcbeida.utils.Utils;
import com.dcbeida.widget.XListView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class AccountListFragment extends BaseFragment implements AccountListContract.View,
        XListView.IXListViewListener, View.OnClickListener, XListView.OnItemClickListener {

    @Bind(R.id.myList)
    XListView myList;
    @Bind(R.id.ll_no_data)
    LinearLayout llNoData;
    private AccountListPresenter mPresent;
    private AccountListAdapter mAdapter;

    private int type = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View initViews() {
        View view = View.inflate(mActivity, R.layout.fragment_account_list, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void initData() {
        super.initData();
        Bundle arguments = getArguments();
        if (arguments != null && arguments.containsKey("type")) {
            type = arguments.getInt("type");
        }
        myList.setPullLoadEnable(false);
        myList.setPullRefreshEnable(true);
        myList.setXListViewListener(this);
        myList.setVerticalScrollBarEnabled(false);// 去掉竖向滚动条
        myList.setOnItemClickListener(this);
        mPresent = new AccountListPresenter(this, mActivity);
        mAdapter = new AccountListAdapter(getActivity());
        myList.setAdapter(mAdapter);
        mPresent.getList(type);

    }

    @Override
    public void showTip(String msg) {
        Utils.showTip(getActivity(), msg);
    }

    @Override
    public void showCustomList(List<Customer> data) {
        myList.stopRefresh();
        mAdapter.clearList();
        if (data == null || data.isEmpty()) {
            myList.setVisibility(View.GONE);
            llNoData.setVisibility(View.VISIBLE);
        } else {
            mAdapter.setmArrayList(data);
            myList.setVisibility(View.VISIBLE);
            llNoData.setVisibility(View.GONE);
        }
    }

    @Override
    public void setPresenter(AccountListContract.Present presenter) {

    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onResume() {
        super.onResume();
        mPresent.getList(type);
    }

    @Override
    public void onRefresh() {
        mPresent.getList(type);
    }

    @Override
    public void onLoadMore() {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Customer customer = mAdapter.getmArrayList().get(position - 1);
        Bundle bundle = new Bundle();
        bundle.putSerializable("customer", customer);
        ((BaseActivity) getActivity()).showActivity(ClerkStatueActivity.class, bundle);
    }
}
